/*
 * Copyright (C) Jochen Hiller, Bernd Kolb.
 * 
 * Licensed under Apache 2.0.
 * See http://www.apache.org/licenses/LICENSE-2.0
 */
package demo.location.tracker;

import static android.provider.BaseColumns._ID;
import static demo.location.tracker.DBConstants.COLUMN_ALTITUDE;
import static demo.location.tracker.DBConstants.COLUMN_DESCRIPTION;
import static demo.location.tracker.DBConstants.COLUMN_LATITUDE;
import static demo.location.tracker.DBConstants.COLUMN_LONGITUDE;
import static demo.location.tracker.DBConstants.COLUMN_TIME;
import static demo.location.tracker.DBConstants.FROM;
import static demo.location.tracker.DBConstants.ORDER_BY;
import static demo.location.tracker.DBConstants.TABLE_NAME;

import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;

public class DBRoute extends SQLiteOpenHelper {

	// db core settings
	private static final String DATABASE_NAME = "route.db";
	private static final int DATABASE_VERSION = 1;

	/** Create a helper object for the Events database */
	public DBRoute(Context ctx) {
		super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_TIME
				+ " INTEGER," + COLUMN_LONGITUDE + " NUMERIC, "
				+ COLUMN_LATITUDE + " NUMERIC, " + COLUMN_ALTITUDE
				+ " NUMERIC, " + COLUMN_DESCRIPTION + " TEXT);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public void deleteLocations() {
		SQLiteDatabase db = getWritableDatabase();
		db.delete(TABLE_NAME, null, null);
		// db.execSQL("DELETE FROM TABLE " + TABLE_NAME);
	}

	public void addLocation(Location loc, String desc) {
		// Insert a new record into the route data source.
		// You would do something similar for delete and update.
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(COLUMN_TIME, System.currentTimeMillis());
		values.put(COLUMN_LONGITUDE, loc.getLongitude());
		values.put(COLUMN_LATITUDE, loc.getLatitude());
		values.put(COLUMN_ALTITUDE, loc.getAltitude());
		values.put(COLUMN_DESCRIPTION, desc);
		db.insertOrThrow(TABLE_NAME, null, values);
	}

	public Cursor getLocations(Activity act) {
		// Perform a managed query. The Activity will handle closing
		// and re-querying the cursor when needed.
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null,
				ORDER_BY);
		act.startManagingCursor(cursor);
		return cursor;
	}

	public String showLocations(Cursor cursor) {
		// Stuff them all into a big string
		StringBuilder builder = new StringBuilder("Saved Locations:\n");
		builder.append("ID, TIME, LAT, LON, ALT, DESC\n");
		while (cursor.moveToNext()) {
			// Could use getColumnIndexOrThrow() to get indexes
			long id = cursor.getLong(0);
			long time = cursor.getLong(1);
			double lon = cursor.getDouble(2);
			double lat = cursor.getDouble(3);
			double altitude = cursor.getDouble(4);
			String desc = cursor.getString(5);
			builder.append(id).append(", ");
			builder.append(
					java.text.DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
							DateFormat.MEDIUM).format(new Date(time))).append(
					", ");
			builder.append(lon).append(", ");
			builder.append(lat).append(", ");
			builder.append(altitude).append(", ");
			builder.append(desc).append("\n");
		}
		return builder.toString();
	}
}